package basic.commons.page;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class ReflectHelper {

	/**
	 * 获取obj对象fieldName的Field
	 * 
	 * @param obj
	 * @param fieldName
	 * @return
	 */
	public static Field getFieldByFieldName(Object obj, String fieldName) {

		for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
			try {
				return superClass.getDeclaredField(fieldName);
			} catch (NoSuchFieldException e) {
			}
		}
		return null;
	}

	/**
	 * 检查是否含有分页或本来就是分页类
	 * 
	 * @param obj
	 * @param fieldName
	 * @return
	 */
	public static Object isPage(Object obj, String fieldName) {

		if (obj instanceof java.util.Map) {
			java.util.Map map = (java.util.Map) obj;
			return map.get(fieldName);

		} else {
			for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass.getSuperclass()) {
				try {
					return superClass.getDeclaredField(fieldName);
				} catch (NoSuchFieldException e) {
				}
			}
			return null;
		}

	}

	/**
	 * 获取obj对象fieldName的属性值
	 * 
	 * @param obj
	 * @param fieldName
	 * @return
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static Object getValueByFieldName(Object obj, String fieldName)
			throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
		Field field = getFieldByFieldName(obj, fieldName);
		Object value = null;
		if (field != null) {
			if (field.isAccessible()) {
				value = field.get(obj);
			} else {
				field.setAccessible(true);
				value = field.get(obj);
				field.setAccessible(false);
			}
		}
		return value;
	}

	/**
	 * 设置obj对象fieldName的属性值
	 * 
	 * @param obj
	 * @param fieldName
	 * @param value
	 * @throws SecurityException
	 * @throws NoSuchFieldException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 */
	public static void setValueByFieldName(Object obj, String fieldName, Object value)
			throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {

		if (obj instanceof java.util.Map) {
			java.util.Map map = (java.util.Map) obj;
			map.put(fieldName, value);
		} else {
			Field field = obj.getClass().getDeclaredField(fieldName);
			if (field.isAccessible()) {
				field.set(obj, value);
			} else {
				field.setAccessible(true);
				field.set(obj, value);
				field.setAccessible(false);
			}
		}

	}

	public static Map<String, Object> getAllMethods(Object obj) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.putAll(getMyMethods(obj));
		map.putAll(getParentMethod(obj));
		return map;
	}

	/**
	 * 获得全部get方法<br/>
	 * 用于Controller向View传递数据
	 * 
	 * @param obj
	 * @return
	 */
	public static Map<String, Object> getMyMethods(Object obj) {
		// Field[] fields = obj.getClass().getDeclaredFields();// 获得属性
		Method[] methods = obj.getClass().getDeclaredMethods();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			for (Method method : methods) {
				String methodName = method.getName();
				if (methodName.startsWith("get")) {
					String refMethodName = (methodName.charAt(3) + "").toLowerCase() + methodName.substring(4);
					// System.out.println("===getMethods=" + refMethodName);
					map.put(refMethodName, method.invoke(obj));
				}
				// System.out.println("===getMethods=" + methodName);
			}

		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return map;
	}

	public static Map<String, Object> getParentMethod(Object obj) {
		// Field[] fields = obj.getClass().getDeclaredFields();// 获得属性
		Method[] methods = obj.getClass().getSuperclass().getDeclaredMethods();
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			for (Method method : methods) {
				String methodName = method.getName();
				if (methodName.startsWith("get")) {
					String refMethodName = (methodName.charAt(3) + "").toLowerCase() + methodName.substring(4).toLowerCase();
					// System.out.println("===getMethods=" + refMethodName);
					map.put(refMethodName, method.invoke(obj));
				}
				// System.out.println("===getMethods=" + methodName);
			}

		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			e.printStackTrace();
		}
		return map;
	}

}
